package com.cee.admin.modules.cfd.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cee.admin.common.BaseController;
import com.cee.common.exception.GlobalException;
import com.cee.common.spring.SpringContextHolder;
import com.cee.common.web.result.Result;
import com.cee.common.web.result.ResultCode;
import com.cee.domain.modules.cfd.entity.CfdCarbonPerformanceEvaluation;
import com.cee.domain.modules.cfd.vo.CfdCarbonPerformanceEvaluationVo;
import com.cee.domain.modules.sys.entity.SysFrontUser;
import com.cee.ds.common.utils.BasePrincipalUtils;
import com.cee.ds.modules.cfd.service.ICfdCarbonPerformanceEvaluationService;
import com.cee.ds.modules.sys.service.ISysFrontUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 * 碳绩效评价表 前端控制器
 * </p>
 *
 * @author autoGenerator
 * @since 2024-04-08
 */
@Tag(name = "产品管理-碳足迹数据管理-碳绩效评价")
@RestController
@RequestMapping("/api/cfd/carbon/performance/evaluation")
public class CfdCarbonPerformanceEvaluationController extends BaseController {

    @Autowired
    private ICfdCarbonPerformanceEvaluationService iCfdCarbonPerformanceEvaluationService;

    @Operation(summary = "分页查询碳绩效评价")
    @GetMapping("/page")
    public Result<Page<CfdCarbonPerformanceEvaluationVo>> page(@RequestParam Long current, @RequestParam Long size, CfdCarbonPerformanceEvaluationVo vo) {
        vo.setCityId(BasePrincipalUtils.currentUser().getCityId());
        handleOrder(vo);
        return success(iCfdCarbonPerformanceEvaluationService.queryPageVo(new Page<>(current,size),vo));
    }

//    @PostMapping("/examine")
//    @Operation(summary = "审核碳绩效评价")
//    public Result<Boolean> examine(@RequestParam Long id, @RequestParam String examineStatus, String examineNote){
//        CfdCarbonPerformanceEvaluation evaluation = new CfdCarbonPerformanceEvaluation();
//        evaluation.setId(id);
//        evaluation.setExamineStatus(examineStatus);
//        evaluation.setExamineNote(examineNote);
//        evaluation.setExamineTime(new Date());
//        evaluation.setExamineUserId(BasePrincipalUtils.currentUserId());
//        return success(iCfdCarbonPerformanceEvaluationService.updateById(evaluation));
//    }

//    @Operation(summary = "删除碳绩效评价")
//    @DeleteMapping("/delete/{id}")
//    public Result<Boolean> delete(@PathVariable("id") Long id) {
//        return success(iCfdCarbonPerformanceEvaluationService.removeById(id));
//    }

//    @Operation(summary = "取消申请碳绩效评价")
//    @DeleteMapping("/cancel/{id}")
//    public Result<Boolean> cancel(@PathVariable("id") Long id) {
//        CfdCarbonPerformanceEvaluation evaluation = new CfdCarbonPerformanceEvaluation();
//        evaluation.setId(id);
//        evaluation.setExamineStatus("4");
//        return success(iCfdCarbonPerformanceEvaluationService.updateById(evaluation));
//    }

    @Operation(summary = "查看碳绩效评价详情")
    @GetMapping("/get/{id}")
    public Result<CfdCarbonPerformanceEvaluation> get(@PathVariable("id") Long id) {
        CfdCarbonPerformanceEvaluation evaluation = iCfdCarbonPerformanceEvaluationService.getById(id);
        Integer cityId = BasePrincipalUtils.currentUser().getCityId();
        if (cityId != null) {
            Long belongUser = evaluation.getBelongUser();
            SysFrontUser frontUser = SpringContextHolder.getBean(ISysFrontUserService.class).getByIdOrEx(belongUser);
            if (!Objects.equals(cityId, frontUser.getCityId())) {
                throw new GlobalException(ResultCode.NOT_PERMISSION);
            }
        }
        return success(evaluation);
    }

}
